﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZuoAlgorithms.Class017;

/// <summary>
/// 递归序的解释
/// 用递归实现二叉树的三序遍历
/// </summary>
public class BinaryTreeTraversalRecursion
{
    public class TreeNode
    {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int v)
        {
            val = v;
        }
    }

    //  递归基本样子，用来理解递归序
    public void F(TreeNode head)
    {
        if (head == null)
        {
            return;
        }
        F(head.left);
        F(head.right);
    }

    // 先序打印所有节点，递归版
    public void PreOrder(TreeNode head)
    {
        if (head == null)
        {
            return;
        }
        Console.WriteLine(head.val + " ");
        PreOrder(head.left);
        PreOrder(head.right);
    }

    // 中序打印所有节点，递归版
    public void InOrder(TreeNode head)
    {
        if (head == null)
        {
            return;
        }
        InOrder(head.left);
        Console.WriteLine(head.val + " ");
        InOrder(head.right);

    }

    // 后序打印所有节点，递归版
    public void PosOrder(TreeNode head)
    {
        if (head == null)
        {
            return;
        }
        PosOrder(head.left);
        PosOrder(head.right);
        Console.WriteLine(head.val + " ");
    }






}
